define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, Logistics_Edit, $stateParams, GoPage, GoDialog) {
		//接收参数
		$scope.id = $stateParams.id;
		$scope.title = $scope.id == "" ? "添加运费模板" : "编辑运费模板";
		var id = new Array();
		var region = new Array();
		$scope.choose = 0;

		$scope.scope = new Object();
		$scope.scope.comp_select = {id:0,name:"请选择物流公司"};
		$scope.scope.company_id = 0; //下拉框默认显示“请选择”的字样
		$scope.scope.after_balance = false;
		$scope.scope.status = false;
		$scope.scope.region_type = 1;
		$scope.scope.base_weight = ''
		$scope.scope.base_weight_obj = {id:0,name:"请选择物流公司"};
		$scope.scope.baseWeight = [{id:1,name:"首要重量500克"},{id:2,name:"首要重量1000克"},{id:3,name:"首要重量500千克"},{id:4,name:"首要重量1000千克"}];
		$scope.scope.add_weight = ''
		$scope.scope.add_weight_obj = {id:0,name:"请选择物流公司"};
		$scope.scope.addWeight = [{id:1,name:"续重单位500克"},{id:2,name:"续重单位1000克"},{id:3,name:"续重单位500千克"},{id:4,name:"续重单位1000千克"}];
		$scope.scope.base_cose = '';
		$scope.doSelectLogist = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择物流公司", $scope.company.logisticscomp, $scope.comp_select, function (data) {
				$scope.scope.company_id = data.id;
				$scope.scope.comp_select = data;
			});
		}
		$scope.doSelectBaseWeight = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择首要重量", $scope.scope.baseWeight, $scope.scope.base_weight_obj, function (data) {
				$scope.scope.base_weight = data.id;
				$scope.scope.base_weight_obj = data;
			});
		}
		$scope.doSelectAddWeight = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择续重单位", $scope.scope.addWeight, $scope.scope.add_weight_obj, function (data) {
				$scope.scope.add_weight = data.id;
				$scope.scope.add_weight_obj = data;
			});
		}
		$scope.flg_choose = function () {
			for (var i = 0; i < $scope.company.logisticscomp.length; i++) {
				if ($scope.company.logisticscomp[i].choosen_flg == true)
					$scope.choose += 1;
			}
			if ($scope.choose < 1) {
				GoDialog.toast('当前无可用物流公司，请物流公司模块选择');
			}
		}
		$scope.submit_data = function () {
			if (!$scope.scope.name) {
				GoDialog.toast('请输入配送方式名称');
				return;
			}
			if ($scope.scope.company_id == 0) {
				GoDialog.toast('请选择物流公司');
				return;
			}
			if (!$scope.kind) {
				GoDialog.toast('请选择模板类型');
				return;
			}
			if ($scope.scope.base_weight == '') {
				GoDialog.toast('请选择首重设置');
				return;
			}
			if (!$scope.scope.add_weight) {
				GoDialog.toast('请选择续重设置');
				return;
			}
			if (!$scope.scope.base_cose && $scope.scope.base_cose != 0) {
				GoDialog.toast('请输入首重费用');
				return;
			}
			if (!$scope.scope.add_cose && $scope.scope.add_cose != 0) {
				GoDialog.toast('请输入续重费用');
				return;
			}
			Logistics_Edit.model_eidt($scope.id, $scope.scope.name, $scope.scope.company_id, $scope.kind, $scope.scope.status, $scope.scope.after_balance, $scope.scope.base_weight, $scope.scope.add_weight, $scope.scope.base_cose, $scope.scope.add_cose, $scope.scope.region_type, function (data) {
				$scope.success = data;
				//				console.log(data);
				if ($scope.success == true) {
					GoDialog.toast("添加成功");
					GoPage.go_states('logistics-modal');
				} else
					GoDialog.toast(data.msg);
			})

		}

		$scope.trans_base_weight = function () {
			$scope.scope.base_weight = $scope.scope.base_weight.toString();
		}
		$scope.trans_add_weight = function () {
			$scope.scope.add_weight = $scope.scope.add_weight.toString();
		}
		$scope.trans_base_cose = function () {
			$scope.scope.base_cose = parseFloat($scope.scope.base_cose);
		}
		$scope.trans_add_cose = function () {
			$scope.scope.add_cose = parseFloat($scope.scope.add_cose);
		}
		$scope.trans_region_type = function () {
			if ($scope.datas.region_type == 1) {
				$scope.scope.region_type = 1;
			} else if ($scope.datas.region_type == 2) {
				$scope.scope.region_type = 2;
			}
		}
		$scope.trans_kind = function () {
			if ($scope.scope.kind == '款到发货') {
				$scope.scope.kind1 = true;
				$scope.scope.kind2 = false;
				$scope.kind = 1;
			} else if ($scope.scope.kind == '货到付款') {
				$scope.scope.kind1 = false;
				$scope.scope.kind2 = true;
				$scope.kind = 2;
			}
		}
		$scope.select_kind = function (index) {
			if (index == 1) {
				$scope.scope.kind1 = true;
				$scope.scope.kind2 = false;
				$scope.kind = 1;
			} else if (index == 2) {
				$scope.scope.kind1 = false;
				$scope.scope.kind2 = true;
				$scope.kind = 2;
			}
		}
		$scope.init_data = function () {
			if ($scope.id != '') {
				Logistics_Edit.company_list(function (data) {
					//					console.log(data);
					$scope.company = data;
					Logistics_Edit.model_detail($scope.id, function (data) {
						$scope.scope = data;
						//						console.log(data);
						$scope.trans_add_cose();
						$scope.trans_base_cose();
						$scope.trans_add_weight();
						$scope.trans_base_weight();
						//				$scope.trans_region_type();
						$scope.trans_kind();
					})
				})
			} else
				Logistics_Edit.company_list(function (data) {
					$scope.company = data;
				})

		}
		$scope.init_data(); //必须在定义之后
	}

	controller.$inject = ['$scope', 'Logistics_Edit', '$stateParams', 'GoPage', 'GoDialog'];

	return app.registerController("logistics_editctrl", controller);
});